import { defHttp } from "/@/utils/http/axios"
import { Modal } from "ant-design-vue"
import { RequestHeaderEnum } from "/@/enums/httpEnum"

enum Api {
  list = "/food-service/table/listTable",
  save = "/food-service/table/saveTable",
  get = "/food-service/table/getTable",
  edit = "/food-service/table/updateTable",
  deleteTable = "/food-service/table/deleteTable",
  deleteBatch = "/food-service/table/batchDeleteTable",

  listCategory = "/food-service/table/listCategory",
  saveCategory = "/food-service/table/saveCategory",
  editCategory = "/food-service/table/updateCategory",
  deleteCategory = "/food-service/table/deleteCategory",
  genQrcode = "/food-service/table/genQrcode",
}

/**
 * 列表接口
 * @param params
 */
export const listTable = (params) => {
  const headers = {}
  headers[RequestHeaderEnum.TENANT_ID] = params.tenantId
  return defHttp.get({ url: Api.list, params, headers })
}

export const listTableCategory = (params) => {
  const headers = {}
  headers[RequestHeaderEnum.TENANT_ID] = params.tenantId
  return defHttp.get({ url: Api.listCategory, params, headers })
}

/**
 * 删除
 */
export const deleteTable = (params, handleSuccess) => {
  const headers = {}
  headers[RequestHeaderEnum.TENANT_ID] = params.tenantId
  return defHttp.delete({ url: Api.deleteTable, params, headers }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

export const deleteCategory = (params, handleSuccess) => {
  const headers = {}
  headers[RequestHeaderEnum.TENANT_ID] = params.tenantId
  return defHttp.delete({ url: Api.deleteCategory, params, headers }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

/**
 * 批量删除
 * @param params
 */
export const batchDeleteTable = (params, handleSuccess) => {
  const headers = {}
  headers[RequestHeaderEnum.TENANT_ID] = params.tenantId
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params, headers }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdateTable = (params, isUpdate) => {
  const headers = {}
  console.log("params.tenantId", params.tenantId)
  headers[RequestHeaderEnum.TENANT_ID] = params.tenantId
  return defHttp.post({ url: isUpdate ? Api.edit : Api.save, params, headers })
}

export const saveOrUpdateCategory = (params, isUpdate) => {
  const headers = {}
  console.log("params.tenantId", params.tenantId)
  headers[RequestHeaderEnum.TENANT_ID] = params.tenantId
  return defHttp.post({ url: isUpdate ? Api.editCategory : Api.saveCategory, params, headers })
}

export const genTableQrcode = (params) => {
  const headers = {}
  headers[RequestHeaderEnum.TENANT_ID] = params.tenantId
  return defHttp.get({ url: Api.genQrcode, params, headers })
}
